﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerCreator.LMS.Common.Script;

public partial class Theme_Index_SelectCourseModle : System.Web.UI.Page
{
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.UserCourse opUserCourse = new PowerCreator.LMS.Web.Course.UserCourse();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        CheckUser();
        //ViewState["cid"] = 0;
        if (!IsPostBack)
        {
            string cid = Request.QueryString["CourseID"].ToString();
            ViewState["cid"] = cid;
            DataBinderPageData(cid);
            BinderClass();
            if (ddl_Class.Items.Count > 0)
            {
                BinderLbl(int.Parse(ddl_Class.SelectedValue));
            }
            else
            {
                lbl_bixiukeshi.Text = "您尚无班级，无法选课，请联系管理员！";
                Button1.Enabled = false;
            }

        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }
        //else
        //{
        //    验证此课件该用户是否可以点播
        //    PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[WebConfig.SESSION_USER_USERINFO];
        //    string courseIDlist = getMyCourseIDlist();
        //}
    }
    private void DataBinderPageData(string cid)
    {
        PowerCreator.LMS.Course.Entity.VOTOCourse vo = new PowerCreator.LMS.Course.Entity.VOTOCourse();
        vo = opCourse.LoadVOTCourse(int.Parse(cid));
        if (vo.Course_Id > 0)
        {
         
            lbl_CourseName.Text = vo.CourseName;
            lbl_cikekeshi.Text = "此课程课时数："+vo.CoursePeriod;
           
        }
    }
    private void BinderClass()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }
        else
        {
            string search = " SELECT TO_Class.Name, TO_Class.Class_Id  FROM TO_UserOfClass ,TO_Class Where TO_UserOfClass.ClassID = TO_Class.Class_Id and TO_UserOfClass.UserID=" + login.UserID;
            DataTable dt = new DataTable();
            dt = obj.Query(search);
            ddl_Class.DataSource = dt;
            ddl_Class.DataTextField = "Name";
            ddl_Class.DataValueField = "Class_Id";
            ddl_Class.DataBind();

        }

    }
    protected void ddl_Class_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderLbl(int.Parse(ddl_Class.SelectedValue));
    }
    private void BinderLbl(int claid)
    {
        int total = 0;
        int total_bixiu = 0;
        int total_xuanxiu = 0;
        int total_shengyu = 0;
        int sum_yixuan = 0;
        string courseidlist="0";
        
        string sql = "SELECT TO_Batch.Batch_xueshi FROM TO_Class INNER JOIN TO_CourseList ON  TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id INNER JOIN TO_Batch ON TO_CourseList.Batch_Id = TO_Batch.Batch_Id where TO_Class.Class_Id="+claid.ToString();
        DataTable dt = obj.Query(sql);
        if (dt.Rows.Count > 0)
        {
            total = int.Parse(dt.Rows[0]["Batch_xueshi"].ToString());
        }
        dt.Clear();
        sql = "SELECT TO_CourseList.CourseID FROM TO_Class INNER JOIN  TO_CourseList ON  TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id where TO_Class.Class_Id=" + claid.ToString();
        dt = obj.Query(sql);
        if (dt.Rows.Count > 0)
        {
            if(dt.Rows[0]["CourseID"].ToString().Length>0)
                courseidlist = dt.Rows[0]["CourseID"].ToString();

        }
        dt.Clear();
        string[] courseidA = courseidlist.Split(',');
        //sql = "SELECT sum(CoursePeriod) as sum from TO_Course where Course_Id in (" + courseidlist + ") ";
        sql = "SELECT isnull(sum(CoursePeriod),0) as sum from TO_Course where Course_Id in ( 0,";
        for (int j = 0; j < courseidA.Length - 1;j++ )
        {
            string[] courseidB = courseidA[j].Split('#');
            sql = sql + courseidB[0].ToString() + ",";
        }
        sql = sql.TrimEnd(',');
        sql = sql + ")";
        dt = obj.Query(sql);
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["sum"] == null)
                total_bixiu = 0;
            else
                total_bixiu = int.Parse(dt.Rows[0]["sum"].ToString());
        }
        total_xuanxiu = total - total_bixiu;
        if (total_xuanxiu < 0)
            total_xuanxiu = 0;
        dt.Clear();
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        sql = "SELECT sum(CoursePeriod) as sum from TO_Course where Course_Id in (select FKUCCourseID from UserCourse where FKUCClassID=" + claid.ToString() + " and UCUserID=" + login.UserID.ToString() + ") ";
        //Response.Write(sql);
        //Response.End();
        dt = obj.Query(sql);
        if (dt.Rows.Count > 0)
        {
            
            if (dt.Rows[0]["sum"] == null)
                sum_yixuan = 0;
            else if(dt.Rows[0]["sum"].ToString().Length==0)
                sum_yixuan = 0;
            else
                sum_yixuan = int.Parse(dt.Rows[0]["sum"].ToString());

        }
        total_shengyu = total_xuanxiu - sum_yixuan;
        if (total_shengyu < 0)
            total_shengyu = 0;
        lbl_totalkeshi.Text = "班级总课时数：" + total;
        lbl_bixiukeshi.Text = "规定的必修课总课时数：" + total_bixiu;
        lbl_xuanxiukeshi.Text = "规定的选修课总课时数：" + total_xuanxiu;
        lbl_yixuanxiukeshi.Text = "已选选修课时数：" + sum_yixuan;
        lbl_shengyukeshi.Text = "剩余选修课时数：" + total_shengyu;
    }

    protected void btn_Close(object sender, EventArgs e)
    {
        Response.Write("<script> window.returnValue='false';window.close();</script>");

        //Script.CloseMe();

    }
    protected void btn_Click(object sender, EventArgs e)
    {
        string search = string.Format(" UCUserID={0} and FKUCCourseID = {1} and FKUCClassID={1}", login.UserID, ViewState["cid"].ToString(),ddl_Class.SelectedValue);
        if (opUserCourse.QueryUserCourse(search) == 0)
        {
            PowerCreator.LMS.Course.Entity.UserCourse voUserCourse = new PowerCreator.LMS.Course.Entity.UserCourse();
            voUserCourse.FKUCCourseID = int.Parse(ViewState["cid"].ToString());
            voUserCourse.UCState = 1;
            voUserCourse.UCUserID = login.UserID;
            voUserCourse.FKUCClassID = int.Parse(ddl_Class.SelectedValue);
            opUserCourse.AddUserCourse(voUserCourse);
           // Script.ShowAndRedirect(this, "选课成功！", url);
            Response.Write("<script> window.returnValue='true';window.close();</script>");
           // Script.CloseMe();
        }


    }
}
